package com.zhangtai.canal;

//import com.alibaba.fastjson.JSONObject;
//import com.alibaba.otter.canal.protocol.CanalEntry;
//import com.xpand.starter.canal.annotation.CanalEventListener;
//import com.xpand.starter.canal.annotation.ListenPoint;
//import com.zhangtai.modules.dto.EmployeeCachDto;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//
//import java.util.regex.Matcher;
//import java.util.regex.Pattern;
//
//@CanalEventListener
//@Slf4j
//public class BusinessListener {
//    @Autowired
//    private RedisTemplate redisTemplate;
//
//    private static Pattern linePattern = Pattern.compile("_(\\w)");
//
//    @ListenPoint(schema = "renren", table = {"tb_employee_info"})
//    public void adUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
//        JSONObject jsonObject = new JSONObject();
//        for (int i = 0; i < rowData.getAfterColumnsCount(); i++) {
//            jsonObject.put(lineToHump(rowData.getAfterColumnsList().get(i).getName()), rowData.getAfterColumnsList().get(i).getValue());
//        }
//        log.info("监听到的sql变化:{}",jsonObject);
//        String employeestatus = jsonObject.getString("employeestatus");
//
//        EmployeeCachDto employeeInfo = JSONObject.parseObject(jsonObject.toString(), EmployeeCachDto.class);
////        离职状态则删除
//        try {
//            if (!"2".equals(employeestatus) && !"9".equals(employeestatus)) {
//                redisTemplate.delete("employeeInfo" + employeeInfo.getCode());
//            } else {
//                redisTemplate.boundValueOps("employeeInfo" + employeeInfo.getCode()).set(employeeInfo);
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
////    @ListenPoint(schema = "renren", table = {"sys_user"})
////    public void userUpdate(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
////        JSONObject jsonObject = new JSONObject();
////        for(int i=0;i<rowData.getAfterColumnsCount();i++){
////            jsonObject.put(lineToHump(rowData.getAfterColumnsList().get(i).getName()),rowData.getAfterColumnsList().get(i).getValue());
////
////        }
//////        log.info("监听到的sql变化:{}",jsonObject);
////        String employeestatus = jsonObject.getString("employeestatus");
////
////
////
////        EmployeeCachDto employeeInfo =  JSONObject.parseObject(jsonObject.toString(), EmployeeCachDto.class);
//////        离职状态则删除
////        try {
////            if(!"2".equals(employeestatus)&&!"9".equals(employeestatus)){
////                redisTemplate.delete("employeeInfo"+employeeInfo.getOid());
////            }else{
////                redisTemplate.boundValueOps("employeeInfo"+employeeInfo.getOid()).set(employeeInfo);
////            }
////        }catch (Exception e){
////            e.printStackTrace();
////        }
////    }
//
//    /**
//     * 8      * 下划线转驼峰
//     * 9
//     */
//    public static String lineToHump(String str) {
//        str = str.toLowerCase();
//        Matcher matcher = linePattern.matcher(str);
//        StringBuffer sb = new StringBuffer();
//        while (matcher.find()) {
//            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
//        }
//        matcher.appendTail(sb);
//        return sb.toString();
//    }
//}
